<? 
	//error_reporting(E_ALL);
	require_once "../../commons/includes/authentication.inc"; 	
	require_once "../../commons/includes/editjs.inc"; 
	require_once "../../commons/includes/stringutils.inc"; 
	
	//recupero il recordset determinando il filtro dalla request
	require_once "../../commons/includes/connection.inc"; 
	
	
	$da = $_REQUEST["from_date"];
	$a = $_REQUEST["to_date"];
	
	
 
	$t_a = split("/",$_REQUEST["from_date"]);
	$t_da = split("/",$_REQUEST["to_date"]);
	
	$dta 	= $t_a [2] 	. "-" .$t_a [1] 	. "-" .$t_a [0];
	$dtda 	= $t_da [2] . "-" .$t_da [1] 	. "-" .$t_da [0];
	
	$sql = "SELECT  codice from Articoli";
	$rs = rs($sql, $_REQUEST,"Maga");
	
	
	while ($values = $rs->getRow()) {
		foreach($values as $key=>$val) $$key = $val;	 
		
		//$sql = "SELECT sum(prezzo*qta)/sum(qta) as prz FROM MovimentiMagazzino WHERE (datamov between #$dta# AND #$dtda#) AND codart='$codice' AND tipomov=1 and qta>0";
		$sql = "SELECT prezzo,qta FROM MovimentiMagazzino WHERE (datamov between #$dta# AND #$dtda#) AND codart='$codice' AND tipomov=1 and qta>0";
		$rsLis = rs($sql, "","Mov");
		
		$tprezzoqta= 0;
		$tqta+= 0;
		$media = 0;
		
		while ($mov_values = $rsLis->getRow()) {
			$tprezzoqta+= $mov_values["prezzo"] * $mov_values["qta"]  ;
			$tqta+= $mov_values["qta"];
		}
		
		@$rsLis->freeResult();
		
		$rsLis = rs($sql, "","Movb");
		while ($mov_values = $rsLis->getRow()) {
			$tprezzoqta+= $mov_values["prezzo"] * $mov_values["qta"]  ;
			$tqta+= $mov_values["qta"];
		}
		@$rsLis->freeResult();
		
		$media = $tprezzoqta/$tqta ; 
		
		if("$media"!=""){
			
			$insert = "	UPDATE Articoli  SET 
					prezzo= $media 
					WHERE  codice='$codice'
			"; 	
			
			$res = sqlExecute($insert,"Maga"); 
			
		}
		
	}
	
	
	@$rs->freeResult();
		
?>
	
	<script>
		document.location = "../../calcolocmed/calcolo/list.php";
	</script>	
</body>
</html>